<?php

/**
 * search actions.
 *
 * @package    tfe
 * @subpackage search;
 * @author     3emeIG
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class searchActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {

    /*$this->idsAtelier = Doctrine_Core::getTable('Atelier')
        ->search('%'.$request->getParameter('query').'%');  

    $this->ateliers = Doctrine_Query::create()
        ->from('Atelier')
        ->whereIn($this->idsAtelier)
        ->execute();*/

  }




    public function executeTrouverContact(sfWebRequest $request) {

//      $wh = "";
//      if($request->getParameter('_search')) {
//       $sarr = $request;
//        foreach( $sarr as $k=>$v) {
//            switch ($k) {
//                case 'Nom': $wh = $v; break;//" AND ".$k." LIKE '%".$v."%'"; break;
//                case 'Prenom':
//                case 'Localite':
//                case 'CodePostal':
//                case 'Commentaire':
//            }
//         }
//       }
        
      $this->contacts = Doctrine_Query::create()
        ->select()
        ->from('Contact c')
        ->where('nom LIKE \'%'.$request->getParameter('recherche').'%\'')
                //->andWhere('nom LIKE \'%'.$wh.'%\'')
        ->orWhere('prenom LIKE \'%'.$request->getParameter('recherche').'%\'')
        ->orWhere('code_postal LIKE \'%'.$request->getParameter('recherche').'%\'')
        ->orWhere('localite LIKE \'%'.$request->getParameter('recherche').'%\'')
        ->orWhere('commentaire LIKE \'%'.$request->getParameter('recherche').'%\'')
        ->execute();

        $inscriptions = array();
        if ($request->isXmlHttpRequest()) {

                if (!is_null($this->contacts) && !empty($this->contacts)) {

                    //$id = 1;

                        foreach ($this->contacts as $contact) {
                            $inscriptions['cellule'][] = array(
                                    'id'        => $contact->getId(),
                                    'cell'      => array($contact->getNom(),$contact->getPrenom(),
                                        $contact->getCodePostal(),$contact->getLocalite()
                                    ,$contact->getCommentaire())
                            );

                            //$id++;
                        }

                    $this->getResponse()->setHttpHeader('Content-Type','application/json; charset=utf-8');
                    $inscriptions = $this->renderText(json_encode($inscriptions));
                }
            }
        // encoder en Jason78 Ouuuuais big up, snoop dogg .. snoop doggy dog !
        return $inscriptions;
    }


    public function executeTrouverAtelier(sfWebRequest $request) {

    $this->ateliers = Doctrine_Query::create()
    ->select()
    ->from('Atelier a')
    ->where('libelle LIKE \'%'.$request->getParameter('recherche').'%\'')
    ->orWhere('commentaire LIKE \'%'.$request->getParameter('recherche').'%\'')
    ->execute();

        $inscriptions = array();
        if ($request->isXmlHttpRequest()) {

                if (!is_null($this->ateliers) && !empty($this->ateliers)) {

                    //$id = 1;

                        foreach ($this->ateliers as $atelier) {
                            $inscriptions['cellule'][] = array(
                                    'id'        => $atelier->getId(),
                                    'cell'      => array($atelier->getLibelle(),

                                                $atelier->getTypeDePublic()." ".$atelier->getAgeMinimum()."-".$atelier->getAgeMaximum()." ans",

                                                $atelier->getHeureDeDebut()."-".$atelier->getHeureDeFin(),

                                                $atelier->getAnimateur()->getNom()." ".$atelier->getAnimateur()->getPrenom(),
                                    
                                                $atelier->getCommentaire())
                            );

                            //$id++;
                        }

                    $this->getResponse()->setHttpHeader('Content-Type','application/json; charset=utf-8');
                    $inscriptions = $this->renderText(json_encode($inscriptions));
                }
            }
        // encoder en Jason78 Ouuuuais big up, snoop dogg .. snoop doggy dog !
        return $inscriptions;
    }


    public function executeTrouverStage(sfWebRequest $request) {

    $this->stages = Doctrine_Query::create()
    ->select()
    ->from('Stage sta')
    ->where('libelle LIKE \'%'.$request->getParameter('recherche').'%\'')
    ->orWhere('commentaire LIKE \'%'.$request->getParameter('recherche').'%\'')
    ->execute();

        $inscriptions = array();
        if ($request->isXmlHttpRequest()) {

                if (!is_null($this->stages) && !empty($this->stages)) {

                    //$id = 1;

                        foreach ($this->stages as $stage) {
                            $inscriptions['cellule'][] = array(
                                    'id'        => $stage->getId(),
                                    'cell'      => array($stage->getLibelle(),

                                                $stage->getTypeDePublic()."
                                                ".$stage->getAgeMinimum()."-".$stage->getAgeMaximum()." ans",

                                                "Du ".$stage->getDateDeDebut()." au ".$stage->getDateDeFin()." Heures : ".$stage->getHeureDebut()."-".$stage->getHeureFin(),

                                                $stage->getAnimateur()->getNom()." ".$stage->getAnimateur()->getPrenom(),

                                                $stage->getCommentaire())
                            );

                            //$id++;
                        }

                    $this->getResponse()->setHttpHeader('Content-Type','application/json; charset=utf-8');
                    $inscriptions = $this->renderText(json_encode($inscriptions));
                }
            }
        // encoder en Jason78 Ouuuuais big up, snoop dogg .. snoop doggy dog !
        return $inscriptions;
    }

    public function executeTrouverCommunication(sfWebRequest $request) {

    $this->communications = Doctrine_Query::create()
    ->select()
    ->from('Communication commu')
    ->where('type LIKE \'%'.$request->getParameter('recherche').'%\'')
    ->orWhere('commentaire LIKE \'%'.$request->getParameter('recherche').'%\'')
    ->execute();

        $inscriptions = array();
        if ($request->isXmlHttpRequest()) {

                if (!is_null($this->communications) && !empty($this->communications)) {

                    //$id = 1;

                        foreach ($this->communications as $communication) {
                            $inscriptions['cellule'][] = array(
                                    'id'        => $communication->getId(),
                                    'cell'      => array($communication->getObjet(),

                                                $communication->getType(),

                                                $communication->getDate(),

                                                $communication->getCommentaire())
                            );

                            //$id++;
                        }

                    $this->getResponse()->setHttpHeader('Content-Type','application/json; charset=utf-8');
                    $inscriptions = $this->renderText(json_encode($inscriptions));
                }
            }
        // encoder en Jason78 Ouuuuais big up, snoop dogg .. snoop doggy dog !
        return $inscriptions;
    }


    public function executeTrouverGroupe(sfWebRequest $request) {

     $this->groupes = Doctrine_Query::create()
    ->select()
    ->from('Groupe grp')
    ->where('libelle LIKE \'%'.$request->getParameter('recherche').'%\'')
    ->orWhere('commentaire LIKE \'%'.$request->getParameter('recherche').'%\'')
    ->orWhere('type LIKE \'%'.$request->getParameter('recherche').'%\'')
    ->execute();

        $inscriptions = array();
        if ($request->isXmlHttpRequest()) {

                if (!is_null($this->groupes) && !empty($this->groupes)) {

                    //$id = 1;

                        foreach ($this->groupes as $groupe) {
                            $inscriptions['cellule'][] = array(
                                    'id'        => $groupe->getId(),
                                    'cell'      => array($groupe->getLibelle(),

                                                $groupe->getType(),
                                                $groupe->getCommentaire(),
                                        )
                            );

                            //$id++;
                        }

                    $this->getResponse()->setHttpHeader('Content-Type','application/json; charset=utf-8');
                    $inscriptions = $this->renderText(json_encode($inscriptions));
                }
            }
        // encoder en Jason78 Ouuuuais big up, snoop dogg .. snoop doggy dog !
        return $inscriptions;
    }


     public function executeTrouverSpectacle(sfWebRequest $request) {

        $this->spectacles = Doctrine_Query::create()
        ->select()
        ->from('Spectacle spect')
        ->where('titre LIKE \'%'.$request->getParameter('recherche').'%\'')
        ->orWhere('commentaire LIKE \'%'.$request->getParameter('recherche').'%\'')
        ->execute();

        $inscriptions = array();
        if ($request->isXmlHttpRequest()) {

                if (!is_null($this->spectacles) && !empty($this->spectacles)) {

                    //$id = 1;

                        foreach ($this->spectacles as $spectacle) {
                            $inscriptions['cellule'][] = array(
                                    'id'        => $spectacle->getId(),
                                    'cell'      => array($spectacle->getTitre(),

                                                $spectacle->getType(),
                                                $spectacle->getAuteur(),
                                                $spectacle->getCommentaire(),
                                        )
                            );

                            //$id++;
                        }

                    $this->getResponse()->setHttpHeader('Content-Type','application/json; charset=utf-8');
                    $inscriptions = $this->renderText(json_encode($inscriptions));
                }
            }
        // encoder en Jason78 Ouuuuais big up, snoop dogg .. snoop doggy dog !
        return $inscriptions;
    }


        public function executeTrouverTache(sfWebRequest $request) {

            $this->taches = Doctrine_Query::create()
            ->select()
            ->from('Tache tache')
            ->where('type LIKE \'%'.$request->getParameter('recherche').'%\'')
            ->orWhere('commentaire LIKE \'%'.$request->getParameter('recherche').'%\'')
            ->execute();

        $inscriptions = array();
        if ($request->isXmlHttpRequest()) {

                if (!is_null($this->taches) && !empty($this->taches)) {

                    //$id = 1;

                        foreach ($this->taches as $tache) {
                            $inscriptions['cellule'][] = array(
                                    'id'        => $tache->getId(),
                                    'cell'      => array($tache->getType(),

                                                $tache->getDate(),
                                                $tache->getHeure(),
                                                $tache->getPriorite(),
                                                $tache->getCommentaire(),
                                        )
                            );

                            //$id++;
                        }

                    $this->getResponse()->setHttpHeader('Content-Type','application/json; charset=utf-8');
                    $inscriptions = $this->renderText(json_encode($inscriptions));
                }
            }
        // encoder en Jason78 Ouuuuais big up, snoop dogg .. snoop doggy dog !
        return $inscriptions;
    }

}
